//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import android.content.Context;
import android.os.Build.VERSION;
import android.text.TextUtils;
import com.baidu.trace.api.entity.AddEntityResponse;
import com.baidu.trace.api.entity.AroundSearchRequest;
import com.baidu.trace.api.entity.AroundSearchResponse;
import com.baidu.trace.api.entity.BoundSearchRequest;
import com.baidu.trace.api.entity.BoundSearchResponse;
import com.baidu.trace.api.entity.DeleteEntityResponse;
import com.baidu.trace.api.entity.DistrictSearchRequest;
import com.baidu.trace.api.entity.DistrictSearchResponse;
import com.baidu.trace.api.entity.EntityListRequest;
import com.baidu.trace.api.entity.EntityListResponse;
import com.baidu.trace.api.entity.FilterCondition;
import com.baidu.trace.api.entity.LocRequest;
import com.baidu.trace.api.entity.OnEntityListener;
import com.baidu.trace.api.entity.PolygonSearchRequest;
import com.baidu.trace.api.entity.PolygonSearchResponse;
import com.baidu.trace.api.entity.SearchRequest;
import com.baidu.trace.api.entity.SearchResponse;
import com.baidu.trace.api.entity.SortBy;
import com.baidu.trace.api.entity.UpdateEntityResponse;
import com.baidu.trace.c.e;
import com.baidu.trace.model.BaseRequest;
import com.baidu.trace.model.CoordType;
import com.baidu.trace.model.LatLng;
import com.baidu.trace.model.LocType;
import com.baidu.trace.model.TraceLocation;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.json.JSONObject;

public final class aa {
    private static String a = "";
    private static f b = null;

    protected static void a(List<LatLng> var0, TreeMap<String, String> var1) {
        if(var0 != null && !var0.isEmpty()) {
            StringBuffer var2 = new StringBuffer("");
            Iterator var4 = var0.iterator();

            while(var4.hasNext()) {
                LatLng var3 = (LatLng)var4.next();
                var2.append(var3.getLatitude()).append(",").append(var3.getLongitude()).append(";");
            }

            if(var2.length() > 0) {
                var2.deleteCharAt(var2.length() - 1);
                var1.put("vertexes", var2.toString());
            }

        }
    }

    protected static void a(FilterCondition var0, TreeMap<String, String> var1) {
        if(var0 != null) {
            StringBuffer var2 = new StringBuffer("");
            if(null != var0.getEntityNames() && !var0.getEntityNames().isEmpty()) {
                var2.append("entity_names:").append(a(var0.getEntityNames()));
            }

            if(var0.getActiveTime() > 0L) {
                var2.append("|active_time").append(":").append(var0.getActiveTime());
            }

            if(var0.getInActiveTime() > 0L) {
                var2.append("|inactive_time").append(":").append(var0.getInActiveTime());
            }

            if(null != var0.getColumns() && !var0.getColumns().isEmpty()) {
                var2.append("|").append(a(var0.getColumns()));
            }

            if(var2.length() > 0 && var2.charAt(0) == 124) {
                var2.replace(0, 1, "");
            }

            if(!TextUtils.isEmpty(var2.toString())) {
                var1.put("filter", var2.toString());
            }

        }
    }

    protected static void a(SortBy var0, TreeMap<String, String> var1) {
        if(var0 != null) {
            StringBuffer var2;
            (var2 = new StringBuffer()).append(var0.getFieldName()).append(":").append(var0.getSortType().name());

            try {
                var1.put("sortby", URLEncoder.encode(var2.toString(), "UTF-8"));
            } catch (UnsupportedEncodingException var3) {
                var1.put("sortby", var2.toString());
            }
        }
    }

    private static String a(List<String> var0) {
        StringBuffer var1 = new StringBuffer();
        Iterator var4 = var0.iterator();

        while(var4.hasNext()) {
            String var2 = (String)var4.next();

            try {
                var1.append(URLEncoder.encode(var2, "UTF-8")).append(",");
            } catch (Exception var3) {
                var1.append(var2).append(",");
            }
        }

        return TextUtils.isEmpty(var1.toString())?"":var1.substring(0, var1.length() - 1);
    }

    private static String a(Map<String, String> var0) {
        StringBuffer var1 = new StringBuffer();
        Iterator var4 = var0.entrySet().iterator();

        while(var4.hasNext()) {
            Entry var2 = (Entry)var4.next();

            try {
                var1.append((String)var2.getKey()).append(":").append(URLEncoder.encode((String)var2.getValue(), "UTF-8")).append("|");
            } catch (Exception var3) {
                var1.append((String)var2.getKey()).append(":").append((String)var2.getValue()).append("|");
            }
        }

        return TextUtils.isEmpty(var1.toString())?"":var1.substring(0, var1.length() - 1);
    }

    public static void a(BaseRequest var0, int var1, boolean var2, int var3, String var4, OnEntityListener var5) {
        int var6 = var0.getTag();
        switch(var1) {
            case 1:
                EntityListResponse var24 = new EntityListResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((EntityListRequest)var0).getCoordTypeOutput(), var24);
                }

                var5.onEntityListCallback(var24);
                return;
            case 2:
                AddEntityResponse var14 = new AddEntityResponse(var6, var3, var4);
                if(var2) {
                    a.b(var4, var14);
                }

                var5.onAddEntityCallback(var14);
                return;
            case 3:
                DeleteEntityResponse var13 = new DeleteEntityResponse(var6, var3, var4);
                if(var2) {
                    a.b(var4, var13);
                }

                var5.onDeleteEntityCallback(var13);
                return;
            case 4:
                UpdateEntityResponse var12 = new UpdateEntityResponse(var6, var3, var4);
                if(var2) {
                    a.b(var4, var12);
                }

                var5.onUpdateEntityCallback(var12);
                return;
            case 5:
                TraceLocation var11 = new TraceLocation(var6, var3, var4);
                if(var2) {
                    var11.setLocType(LocType.NET_WORK);
                    TraceLocation var19 = var11;
                    String var21 = var4;

                    try {
                        JSONObject var22;
                        if(a.b(var21, var19) && (var22 = new JSONObject(var21)).has("result")) {
                            JSONObject var23;
                            if((var22 = var22.getJSONObject("result")).has("content")) {
                                if((var23 = var22.getJSONObject("content")).has("point")) {
                                    JSONObject var25;
                                    if((var25 = var23.getJSONObject("point")).has("y") && !TextUtils.isEmpty(var25.getString("y"))) {
                                        var19.setLatitude(Double.parseDouble(var25.getString("y")));
                                    }

                                    if(var25.has("x") && !TextUtils.isEmpty(var25.getString("x"))) {
                                        var19.setLongitude(Double.parseDouble(var25.getString("x")));
                                    }
                                }

                                if(var23.has("floor")) {
                                    var19.setFloor(var23.getString("floor"));
                                }

                                if(var23.has("indoor")) {
                                    var19.setIndoor(var23.getString("indoor"));
                                }

                                if(var23.has("bldg")) {
                                    var19.setBuilding(var23.getString("bldg"));
                                }

                                if(var23.has("radius") && !TextUtils.isEmpty(var23.getString("radius"))) {
                                    var19.setRadius(Double.parseDouble(var23.getString("radius")));
                                }
                            }

                            if(var22.has("result") && (var23 = var22.getJSONObject("result")).has("time")) {
                                var19.setTime(var23.getString("time"));
                            }

                            var19.setCoordType(CoordType.bd09ll);
                        }
                    } catch (Exception var9) {
                        ;
                    } finally {
                        ;
                    }
                }

                var5.onReceiveLocation(var11);
                return;
            case 6:
                SearchResponse var20 = new SearchResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((SearchRequest)var0).getCoordTypeOutput(), var20);
                }

                var5.onSearchEntityCallback(var20);
                return;
            case 7:
                BoundSearchResponse var18 = new BoundSearchResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((BoundSearchRequest)var0).getCoordTypeOutput(), var18);
                }

                var5.onBoundSearchCallback(var18);
                return;
            case 8:
                AroundSearchResponse var17 = new AroundSearchResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((AroundSearchRequest)var0).getCoordTypeOutput(), var17);
                }

                var5.onAroundSearchCallback(var17);
                return;
            case 9:
                PolygonSearchResponse var16 = new PolygonSearchResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((PolygonSearchRequest)var0).getCoordTypeOutput(), var16);
                }

                var5.onPolygonSearchCallback(var16);
                return;
            case 10:
                DistrictSearchResponse var15 = new DistrictSearchResponse(var6, var3, var4);
                if(var2) {
                    a.a(var4, ((DistrictSearchRequest)var0).getCoordTypeOutput(), var15);
                }

                var5.onDistrictSearchCallback(var15);
            default:
        }
    }

    protected static void a(Context var0, LocRequest var1, OnEntityListener var2) {
        if(b == null) {
            b = new f(var0);
        }

        if(VERSION.SDK_INT >= 23) {
            boolean var3 = e.e(var0);
            boolean var4 = e.f(var0);
            boolean var5 = e.g(var0);
            if((!var4 || !var5) && !var3) {
                a.a("BaiduTraceSDK", "Need ACCESS_WIFI_STATE or ACCESS_FINE_LOCATION permission to get wifi information");
                TraceLocation var6 = new TraceLocation(var1.getTag(), 14001, "请求失败");
                var2.onReceiveLocation(var6);
                return;
            }
        }

        if(!e.d(var0) || TextUtils.isEmpty(a)) {
            a = b.a();
        }

        if(TextUtils.isEmpty(a)) {
            TraceLocation var7 = new TraceLocation(var1.getTag(), 14001, "请求失败");
            var2.onReceiveLocation(var7);
        } else {
            a.a(var1, a, var2);
        }
    }
}
